<template>
    <div>
        <Modal v-model="affirmModal" title="确认操作" width="20" @on-ok="handleSubmit">
            <div style="text-align:center"> <p>是否确认保存!</p> </div>
        </Modal>
        <Button v-if="showBackBtn" class="goBack" type="primary" @click="$router.back()"><Icon type="ios-arrow-back" /> 返回  </Button>
        <Card class="line-style" v-for="(item,index) in streetList" :key="item.id">
            <div class="line-wrap">
                <div class="line-title">{{item.street_name}}</div>
                <div class="line-main">
                    <Form :ref="'streetList'+item.street_id" :model="item" :rules="formRules"  :label-width="100">
                        <Row>
                            <Col span="7">
                                <FormItem label="项目数量" prop="total_count">
                                    <Input type="number" number v-model="item.total_count" placeholder="输入项目数量" />
                                </FormItem>
                            </Col>
                            <Col span="7">
                                <FormItem  label="复工比例" prop="fg_ratio">
                                    <Input type="number" number  v-model="item.fg_ratio" placeholder="输入复工比例" >
                                        <span slot="append">%</span>
                                    </Input>
                                </FormItem>
                            </Col>
                            <!--                            <Col span="7">-->
                            <!--                                <FormItem label="项目内容" prop="content">-->
                            <!--                                    <Input  v-model="item.content" placeholder="输入项目内容" />-->
                            <!--                                </FormItem>-->
                            <!--                            </Col>-->
                            <!--                            <Col span="7">-->
                            <!--                                <FormItem  label="项目金额" prop="total_amount">-->
                            <!--                                    <Input   type="number" number v-model="item.total_amount" placeholder="输入项目金额" />-->
                            <!--                                </FormItem>-->
                            <!--                            </Col>-->
                            <Col span="7">
                                <FormItem label="上报日期" prop="imp_date">
                                    <DatePicker @on-change="dateChange($event,index)" :value="item.imp_date"  :options="timeLimit"
                                                format="yyyy-MM-dd" type="date" placeholder="选择上报日期" ></DatePicker>
                                </FormItem>
                            </Col>
                        </Row>
                    </Form>
                    <div class="line-button" v-if="!showDetail">
                        <Button type="primary" @click="validateForm('streetList'+item.street_id,index)">保存</Button>
                    </div>
                </div>
            </div>
        </Card>

    </div>
</template>

<script>
import { getStreetData } from '@/api/outbreak-management/public'
import { insert, findList, update } from '@/api/outbreak-management/resume-work/project-progress'

// 街道疫情概览
export default {
  name: 'addPage',
  data () {
    const validateInt = (rule, value, callback) => {
      if (value % 1 !== 0) {
        callback(new Error('请输入整数'))
      } else {
        callback()
      }
    }
    const validateFloat = (rule, value, callback) => {
      const reg = /^(0|\+?[1-9][0-9]{0,8})(\.\d{1,2})?$/
      if (!reg.test(value)) {
        callback(new Error('最多只允许两位小数'))
      } else {
        callback()
      }
    }
    return {
      // 时间限制 不可选择当天之前
      timeLimit: {
        disabledDate (date) {
          return date && date.valueOf() > Date.now()
        }
      },
      // 提示框
      affirmModal: false,
      // 控制保存按钮显隐
      showDetail: false,
      // 控制回退按钮显示
      showBackBtn: false,
      // 街道列表
      streetList: [],
      currentIndex: '',
      // 表单规则
      formRules: {
        // content: [
        //   { required: true, message: '项目内容必填', trigger: 'blur' }
        // ],
        total_count: [
          { required: true, type: 'number', message: '项目数量必填', trigger: 'blur' },
          { validator: validateInt, trigger: 'blur' }
        ],
        fg_ratio: [
          { required: true, type: 'number', message: '复工比例必填', trigger: 'blur' },
          { min: 0, max: 100, type: 'number', message: '请输入正确复工比例', trigger: 'blur' },
          { validator: validateFloat, trigger: 'blur' }
        ],
        // total_amount: [
        //   { required: true, type: 'number', message: '项目金额必填', trigger: 'blur' },
        //   { type: 'number', message: '项目金额填写错误', trigger: 'blur' }
        // ],
        imp_date: [
          { required: true, message: '上报时间必填', trigger: 'blur' }
        ]
      },
      // 编辑的时间
      editDate: false
    }
  },
  created () {
    this.isEditStatus()
  },
  methods: {
    // 日期转字符串
    dateChange (date, index) {
      for (let i = 0; i < this.streetList.length; i++) {
        this.streetList[i].imp_date = date
      }
    },
    // 重置
    handleReset (name) {
      this.$refs[name].resetFields()
    },
    // 校验保存数据
    validateForm (name, index) {
      this.$refs[name][0].validate((valid) => {
        if (valid) {
          this.currentIndex = index
          this.affirmModal = true
        } else {
          this.$Message.error('表单验证失败,请检查后再试!')
        }
      })
    },
    // 保存
    handleSubmit (name) {
      let streetList = this.streetList[this.currentIndex]
      if (this.showBackBtn) {
        this.handleEdit(streetList)
      } else {
        this.handleAdd(streetList)
      }
    },
    // 新增
    handleAdd (streetList) {
      insert(streetList).then(({ data }) => {
        let { errcode } = data
        if (errcode === 0) {
          this.$Message.success('保存成功!')
          this.currentIndex = ''
        } else {
          this.$Message.warning('操作失败!')
        }
      })
    },
    // 修改
    handleEdit (streetList) {
      update(streetList).then(({ data }) => {
        let { errcode } = data
        if (errcode === 0) {
          this.$Message.success('更新成功!')
          this.currentIndex = ''
        } else {
          this.$Message.warning('操作失败!')
        }
      })
    },
    // 获取街道列表
    getStreetData () {
      getStreetData().then(({ data }) => {
        let { data: res, errcode } = data
        if (errcode === 0 && res.length > 0) {
          let streetList = []
          res.forEach(item => {
            let { id, pid, street } = item
            let formObj = {
              street_id: id,
              area_id: pid,
              street_name: street,
              total_count: 0,
              fg_ratio: 0,
              imp_date: '' }
            streetList.push(formObj)
          })
          this.streetList = streetList
          console.log(this.streetList)
        }
      })
    },
    // 获取修改信息
    getEditInfo () {
      findList({ imp_date: this.editDate }).then(res => {
        let { errcode, data } = res.data
        if (errcode === 0) {
          let streetList = []
          data.forEach(item => {
            item.imp_date = this.$common.moment(item.imp_date).format('YYYY-MM-DD')
            item.total_count = parseInt(item.total_count)
            item.fg_ratio = Number(item.fg_ratio)
            streetList.push(item)
          })
          this.streetList = streetList
        } else {
          this.$Message.success('获取信息失败!')
        }
      })
    },
    // 修改状态
    isEditStatus () {
      if (this.$route.query && this.$route.query.imp_date) {
        this.showBackBtn = true
        this.editDate = this.$route.query.imp_date
        this.getEditInfo()
        if (this.$route.query.showDetail) {
          this.showDetail = true
        }
      } else {
        this.getStreetData()
      }
    }
  }
}
</script>

<style scoped> </style>
